/**
 * Created by liubin on 2017/4/27.
 */


var DzpkCardLayer = cc.Layer.extend({

    mCardLayer : null,
    mCardss : null,

    mChipLayer : null,

    //每个位置下注的筹码
    mPerSeatBetChips : null,

    //每个筹码池下注的筹码
    mPerPotBetChips : null,

    mPublicCardss : null,

    mPublicCardStep : null,

    ctor : function() {
        this._super();

        //chips
        this.mChipLayer = new cc.Layer();
        this.addChild(this.mChipLayer);

        //card
        this.mCardLayer = new cc.Layer();
        this.addChild(this.mCardLayer);

        this.mPerSeatBetChips = [[], [], [], [], []];

        this.mPerPotBetChips = [[], [], [], [], []];

        this.mCardss = [];
        for (var i = 0; i < 10; i++){
            var card = new DzpkCard();
            card.setVisible(false);
            card.setTag(i);
            this.mCardLayer.addChild(card, 10-i);

            this.mCardss.push(card);
        }

        this.mPublicCardss = [];
        for (var i = 0; i < 5; i++){

            var card = new DzpkCard();
            card.setVisible(false);
            card.setCardNum(0);
            card.setCardKind(2);
            card.setScale(1.1);
            this.mCardLayer.addChild(card);

            var x = mo.gameSize.x/2 - 95 * 2 + 95 * i;
            var y = mo.gameSize.y/2 + 10;
            card.setPosition(cc.p(x,y));

            this.mPublicCardss.push(card);
        }

        this.mPublicCardStep = 0;
    },

    reset : function () {
        this.mPublicCardStep = 0;
        this.mChipLayer.removeAllChildren();

        for (var i=0; i<10; i++){
            var card = this.mCardss[i];
            card.setVisible(false);
            card.reset();
            card.setPosition(cc.p(-1000, -1000));
        }

        for (var i=0; i<5; i++){
            var card = this.mPublicCardss[i];
            card.setVisible(false);
            card.reset();
        }

        for (var i=0; i<5; i++){
            this.mPerSeatBetChips[i] = [];
            this.mPerPotBetChips[i] = [];
        }
    },

    //发牌动画
    fapai : function(seats, cardNums, cardTypes){
        //初始位置
        var startP = mo.dzpkCardHelper.getWellPoint();

        for (var i=0; i<2; i++){
            for (var j=0; j<5; j++){
                var isShow = seats[j];

                var card = this.mCardss[i*5 + j];
                card.stopAllActions();
                card.setVisible(isShow);
                if(isShow){
                    card.setCardNum(0);

                    if(j == 0){
                        card.setCardKind(3);
                    }else{
                        card.setCardKind(1);
                    }

                    card.setPosition(startP);
                    card.setRotation(0);
                    startP.y -= 0.5;
                }
            }
        }

        var index = 0;
        for(var i = 0; i < 2; i++){
            for(var j = 0; j < 5; j++){
                var isShow = seats[j];
                if (!isShow){
                    continue;
                }

                var toPos = mo.dzpkCardHelper.getInhandCardPos(j);
                var card = this.mCardss[i*5 + j];

                if(j == 0){
                    toPos = cc.p(toPos.x + 30, toPos.y + 30);
                }

                card.runAction(cc.sequence(
                    cc.delayTime(index * 0.2),
                    cc.callFunc(function(){
                        this.setRotation(-360);
                    }, card),
                    cc.spawn(
                        cc.moveTo(0.2, cc.p(toPos.x+10-10*i, toPos.y)),
                        cc.rotateBy(0.2, 365 - 10*i)
                    )
                ));
                index++;
            }
        }

        this.runAction(cc.sequence(
            cc.delayTime(2.0),
            cc.callFunc(function(){
                this.fanpai(cardNums, cardTypes);
            }, this)
        ));

    },

    fanpai : function (cardNums, cardTypes) {
        //翻牌
        for (var i=0; i<10; i++){
            if(cardNums[i%5] == null){
                continue;
            }
            var card = this.mCardss[i];
            if(card != null){
                if(mo.dzpkCardHelper.getExistCardNum(card.mCardNum)){
                    continue;
                }
                var seat = i % 5;
                var index = parseInt(i/5);
                var num = cardNums[seat][index];
                if(num == 0 || num == undefined){
                    continue;
                }

                card.setCardNum(num);
                if(i == 0 || i == 5){
                    //自己的牌
                    card.fanPai(3);
                }else{
                    card.fanPai(1);
                }

                if(card.getTag() >= 5){
                    var pos = card.getPosition();
                    card.setPosition(pos.x-10, pos.y+5);
                }
            }
        }

        //设置牌型
        mo.dzpkTable.mDzpkUILayer.setCardTypeText(cardTypes);
    },


    //显示公共牌
    showPublicCards : function (step, TableCards) {

        this.mPublicCardStep = step;

        var cardIndexs = [];

        if (step == 1){
            cardIndexs = [0,1,2];
        }else if (step == 2){
            cardIndexs = [0,1,2,3];
        }else if (step == 3){
            cardIndexs = [0,1,2,3,4];
        }

        for(var i=0; i<cardIndexs.length; i++){
            var index = cardIndexs[i];
            if(TableCards.length <= index){
                return;
            }

            var card = this.mPublicCardss[index];
            if(card.isVisible()){
                continue;
            }
            card.setVisible(true);

            var cardNum = TableCards[index];
            card.setCardNum(cardNum);
            card.fanPai(2);
        }
    },

    showPublicCardsByCards : function (publicCardNums) {
        var perCardDelayTime = 1.5;
        var repeat = 3 - this.mPublicCardStep;
        this.schedule(function () {
            if(this.mPublicCardStep < 3 && repeat > 0){
                this.showPublicCards(this.mPublicCardStep+1, publicCardNums);
            }
        }, perCardDelayTime, repeat);
    },
    

    showMaxCardType : function (MaxCards, cardType) {

        // var seatuser = mo.dzpkTableStatus.mTableStatus["SeatUser" + mo.dzpkTableStatus.mTableStatus.MySeat];
        // var MaxCards = seatuser.MaxCards;
        // var cardType = seatuser.Cardtype;

        var lightCardNums = mo.dzpkCardHelper.getMaxCardArray(MaxCards, cardType);

        var myCardArray = [];

        myCardArray.push(this.mCardss[0]);
        myCardArray.push(this.mCardss[5]);
        for (var i=0; i<this.mPublicCardss.length; i++){
            myCardArray.push(this.mPublicCardss[i]);
        }

        for (var i=0; i<myCardArray.length; i++){
            var myCard = myCardArray[i];
            if(myCard.mCardNum == 0){
                continue;
            }

            myCard.mCardlight.setVisible(false);

            for (var j=0; j<lightCardNums.length; j++){
                var lightCardNum = lightCardNums[j];
                if(myCard.mCardNum == lightCardNum){
                    myCard.mCardlight.setVisible(true);
                    break;
                }
            }
        }
    },

    //下注飞筹码动画
    betChip : function (actionCode, seat, thisPayMomey, oldPayMomey) {
        var playerPos = mo.dzpkCardHelper.getPlayerPos(seat);
        var chipPos = mo.dzpkCardHelper.getChipPos(seat);

        var payMoney = thisPayMomey - oldPayMomey;
        var chipLevelArray = mo.dzpkCardHelper.getChipLevelArray(payMoney);

        for (var i=0; i<chipLevelArray.length; i++){
            var chip = new DzpkChip();
            chip.setChipLevel(chipLevelArray[i]);
            this.mChipLayer.addChild(chip);

            var hasSetChipNum = this.mPerSeatBetChips[seat].length;
            chip.setTag(hasSetChipNum + 1);
            if(hasSetChipNum + 1 > 5){
                hasSetChipNum = 0;
                chip.setLocalZOrder(-1);
            }

            chip.setPosition(playerPos);
            var toPos = cc.p(chipPos.x, chipPos.y + hasSetChipNum * 4);

            var _chip = chip;

            _chip.runAction(cc.sequence(
                cc.delayTime(0.05*i),
                cc.moveTo(0.2, toPos),
                cc.callFunc(function(_chip){
                    if(_chip.getTag() >= 5){
                        _chip.removeFromParent();
                    }
                }, _chip, _chip)
            ));

            if(chip.getTag() < 5){
                this.mPerSeatBetChips[seat].push(chip);
            }
        }

        this.runAction(cc.sequence(
            cc.delayTime(0.05*chipLevelArray.length + 0.1),
            cc.callFunc(function(){
                mo.dzpkTable.mDzpkUILayer.setBetChipNum(seat, thisPayMomey);
            }, this)
        ));
    },

    //筹码飞进筹码池的动画
    chipToPot : function (pots) {

        var potPos = mo.dzpkCardHelper.getChipPotPos(0);

        for (var seat = 0; seat < 5; seat++) {
            for (var j=0; j< this.mPerSeatBetChips[seat].length; j++){
                var chip = this.mPerSeatBetChips[seat][j];
                if (chip == null) {
                    continue;
                }

                chip.runAction(cc.sequence(
                    cc.delayTime(0.05 * j),
                    cc.spawn(
                        cc.moveTo(0.2, potPos),
                        cc.fadeOut(0.2)
                    ),
                    cc.callFunc(function (_chip) {
                        _chip.removeFromParent();
                    }, chip, chip)
                ));
            }
        }

        this.mPerSeatBetChips = [[],[],[],[],[]];

        // var curPots = mo.dzpkTableStatus.mTableStatus.Pot;
        var curPots = pots;
        var potIndex = 0;
        var totalPot = 0;
        for (var i=0; i<curPots.length; i++){
            var TablePot = curPots[i];
            if(TablePot == null){
                continue;
            }

            if(TablePot.MoneyInPot > 0){
                this.addChipToPos(potIndex, TablePot.MoneyInPot);
                potIndex += 1;
            }

            totalPot += TablePot.MoneyInPot;
        }

        mo.dzpkTable.mDzpkUILayer.setTotalPot(totalPot);
    },

    addChipToPos : function (index, moneyInPot) {

        mo.dzpkTable.mDzpkUILayer.setChipPotNum(index, moneyInPot);

        for (var i=0; i<this.mPerPotBetChips[index].length; i++){
            var chip = this.mPerPotBetChips[index][i];
            if(chip == null){
                continue;
            }
            chip.removeFromParent();
        }

        this.mPerPotBetChips[index] = [];

        var potPos = mo.dzpkCardHelper.getChipPotPos(index);
        var chipLevelArray = mo.dzpkCardHelper.getChipLevelArray(moneyInPot);

        for (var i=0; i<chipLevelArray.length; i++){
            var chip = new DzpkChip();
            chip.setChipLevel(chipLevelArray[i]);
            this.mChipLayer.addChild(chip);

            var hasSetChipNum = this.mPerPotBetChips[index].length;
            if(hasSetChipNum + 1 > 5){
                return;
            }
            var toPos = cc.p(potPos.x - 45, potPos.y + hasSetChipNum * 4);
            chip.setPosition(toPos);
            this.mPerPotBetChips[index].push(chip);
        }
    },


    //断线重连
    setReconnectStatus : function(tableStatus){

        if(tableStatus.Step == 1){

            //桌面公共牌
            this.showPublicCards(tableStatus.Round - 1, tableStatus.TableCards);

            //筹码池
            var pot = tableStatus.Pot;
            var index = 0;
            for (var i = 0; i < pot.length; i++) {
                var TablePot = pot[i];
                if(TablePot == null){
                    continue;
                }
                var moneyInPot = TablePot.MoneyInPot;
                if(moneyInPot > 0){
                    this.addChipToPos(index, moneyInPot);
                    index += 1;
                }

            }

            var seatUsers = [tableStatus.SeatUser1, tableStatus.SeatUser2, tableStatus.SeatUser3, tableStatus.SeatUser4, tableStatus.SeatUser5];
            for (var seat = 0; seat < 5; seat++) {
                var logicSeat = mo.dzpkTableStatus.getLogicForView(seat, tableStatus.MySeat - 1);
                var seatUser = seatUsers[logicSeat];
                if (seatUser == null) {
                    continue;
                }

                if(seatUser.State == 0){
                    mo.dzpkTable.mDzpkUILayer.foldState(seat, true);
                    continue;
                }

                //放上玩家的手牌
                var HandCards = seatUser.HandCards;
                for (var i=0; i<2; i++){
                    var card = this.mCardss[seat + i*5];
                    card.setVisible(true);
                    card.setCardNum(HandCards[i]);
                    card.setCardKind(1);
                    card.setRotation(365 - 10 * i);
                    var cardPos = mo.dzpkCardHelper.getInhandCardPos(seat);
                    card.setPosition(cc.p(cardPos.x+10-10*i, cardPos.y));
                    if(logicSeat == tableStatus.MySeat-1){
                        card.setPosition(cc.p(cardPos.x+10-18*i+30, cardPos.y+5*i+30))
                    }
                }

                //放上当前下注的筹码
                var PayMoney = seatUser.PayMoneyOfRound;
                if(PayMoney > 0){
                    var chipPos = mo.dzpkCardHelper.getChipPos(seat);

                    var chipLevelArray = mo.dzpkCardHelper.getChipLevelArray(PayMoney);

                    for (var i=0; i<chipLevelArray.length; i++){
                        var chip = new DzpkChip();
                        chip.setChipLevel(chipLevelArray[i]);
                        this.mChipLayer.addChild(chip);
                        var hasSetChipNum = this.mPerSeatBetChips[seat].length;
                        var toPos = cc.p(chipPos.x, chipPos.y + hasSetChipNum * 4);
                        chip.setPosition(toPos);
                        this.mPerSeatBetChips[seat].push(chip);
                    }

                    mo.dzpkTable.mDzpkUILayer.setBetChipNum(seat, PayMoney);
                }

                //牌型
                if(seat == 0){
                    var Cardtype = seatUser.Cardtype;
                    mo.dzpkTable.mDzpkUILayer.setCardTypeTextOfSeat(Cardtype, seat);

                    this.showMaxCardType(seatUser.MaxCards, Cardtype);
                }
            }
        }
    },

});
